package cn.yangliu.task.config;

import cn.yangliu.task.handler.TaskUpdateHandler;
import cn.yangliu.task.handler.TaskUpdateHandlerImpl;
import cn.yangliu.task.lock.Locker;
import cn.yangliu.task.locker.RedisLocker;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.task.cluster")
public class TaskClusterProperties {

    /**
     * 锁过期时间 默认10秒
     * 建议将过期时间设置得比服务器时间差稍大00
     */
    private long lockExpireTime = 10000;

    private Class<? extends Locker> lockerImplClass = RedisLocker.class;

    private Class<? extends TaskUpdateHandler> taskUpdateHandlerClass = TaskUpdateHandlerImpl.class;

    /**
     * 当任务结束后(正常与非正常)是否主动释放锁,
     * 默认不释放(避免因集群环境服务器时间差导致任务重复执行)
     * 比如A服务器比B服务器快10秒钟 任务只需要1秒钟 A先执行,如A执行完后释放了锁资源
     * 理论上9秒钟后B也会执行一次,造成任务重复执行
     */
    private boolean releaseLock = false;

    public Class<? extends TaskUpdateHandler> getTaskUpdateHandlerClass() {
        return taskUpdateHandlerClass;
    }

    public void setTaskUpdateHandlerClass(Class<? extends TaskUpdateHandler> taskUpdateHandlerClass) {
        this.taskUpdateHandlerClass = taskUpdateHandlerClass;
    }

    public long getLockExpireTime() {
        return lockExpireTime;
    }

    public void setLockExpireTime(long lockExpireTime) {
        this.lockExpireTime = lockExpireTime;
    }

    public Class<? extends Locker> getLockerImplClass() {
        return lockerImplClass;
    }

    public void setLockerImplClass(Class<? extends Locker> lockerImplClass) {
        this.lockerImplClass = lockerImplClass;
    }

    public boolean isReleaseLock() {
        return releaseLock;
    }

    public void setReleaseLock(boolean releaseLock) {
        this.releaseLock = releaseLock;
    }
}
